#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
LL a[maxn];
LL cnt[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--){
    LL n,x;cin>>n>>x;
    LL sum=0;
    for(LL i=0;i<=n+10;i++) a[i]=cnt[i]=0;
    for(LL i=1;i<=n;i++) {
        cin>>a[i];
        sum+=a[i];
    }
    for(LL i=1;i<=n;i++){
        LL temp=a[i];
        while(temp&&temp%x==0){
            temp/=x;
            cnt[i]++;
        }
    }
    ///debug(cnt[1]);
    LL pos=1;LL mx=1e18;
    for(LL i=1;i<=n;i++){
        if(mx>cnt[i]){
            pos=i;
            mx=cnt[i];
        }
    }
  /// debug(pos);
  /// debug(mx);
    for(LL i=1;i<pos;i++){
        ///cout<<cnt[i]<<" "<<a[i]<<endl;
        sum+=(mx+1)*a[i];
    }
    for(LL i=pos;i<=n;i++){
        sum+=mx*a[i];
    }
    cout<<sum<<endl;
  }
return 0;
}
